<template>
  <div>
    <div class="action-bar">
      <!-- <el-button v-if="status === 'doing'" type="primary" size="mini" icon="el-icon-plus" @click="dialogGainFormVisible = true">添加盘盈</el-button> -->
      <el-upload
        class="upload-demo"
        :headers="headers"
        :action="importUrl"
        :on-success="handleSuccess"
        :on-error="handleError"
        :multiple="false"
        :show-file-list="false"
      >
        <el-button size="small" type="primary" icon="el-icon-upload2">导入盘点结果</el-button>
      </el-upload>
      <!-- <el-button size="mini" icon="el-icon-back" @click="toBack">返回</el-button> -->
      <!-- <el-button type="primary" size="mini" icon="el-icon-back" @click="$parent.deal($parent.selections)">盘点处理</el-button> -->
      <el-button size="mini" icon="el-icon-download" @click="exportExcel">导出盘点结果</el-button>
    </div>
    <FormDialog title="新建" :show="dialogFormVisible" width="40%" @on-ok="ok" @on-cancel="cancel">
      <EditForm ref="editForm" />
    </FormDialog>
    <FormDialog
      title="新建盘盈资产"
      :show="dialogGainFormVisible"
      width="90%"
      class="fixed-dialog-height"
      @on-ok="gainOk"
      @on-cancel="gainCancel"
    >
      <GainForm ref="gainForm" />
    </FormDialog>
  </div>
</template>
<script>
import {
  exportDetail,
  importExcelUrl
} from '@neu/dcam-ui/api/inventoryDuty'
import FormDialog from '@neu/biz-core/components/FormDialog'
// 编辑实例组件独立编写，需要传递id属性，判断新建还是更新操作
import EditForm from './editForm'
import GainForm from './gainForm'
import { getToken } from '@neu/biz-core/utils/auth'

export default {
  name: 'InventoryExecuteResultActionBar',
  components: {
    FormDialog,
    EditForm,
    GainForm
  },
  props: {
    refresh: {
      required: true,
      type: Function
    }
  },
  data() {
    return {
      dialogFormVisible: false,
      dialogGainFormVisible: false,
      status: this.$route.query.status ? this.$route.query.status : '',
      headers: {
        'Access-Control-Allow-Origin': '*',
        'Authentication': getToken()
      }
    }
  },
  computed: {
    importUrl: function() {
      return importExcelUrl(this.$attrs['duty-id'])
    }
  },
  methods: {
    // 新建用户按钮点击处理
    add() {
      this.dialogFormVisible = true
    },
    // 创建用户对话框确认事件处理
    ok() {
      // 直接调用表单实例的保存方法
      this.$refs.editForm.save().then(response => {
        if (response === false) return
        this.$message({
          showClose: true,
          message: '操作成功！',
          type: 'success'
        })
        // 调用pageable-table实例的mixin方法refresh()，更新表格
        this.refresh()
        // 关闭对话框
        this.dialogFormVisible = false
        this.$refs.editForm.reset()
      })
    },
    cancel() {
      this.$refs.editForm.reset()
      this.dialogFormVisible = false
    },
    // 返回任务列表
    toBack() {
      this.$router.go(-1)
    },
    // 资产盘盈ok
    gainOk() {
      // 直接调用表单实例的保存方法
      this.$refs.gainForm.save().then(response => {
        if (response === false) return
        this.$message({
          showClose: true,
          message: '操作成功！',
          type: 'success'
        })
        // 调用pageable-table实例的mixin方法refresh()，更新表格
        this.refresh()
        // 刷新界面
        this.$refs.gainForm.reset()
        // 关闭对话框
        this.dialogGainFormVisible = false
      })
    },
    // 资产盘盈取消
    gainCancel() {
      this.dialogGainFormVisible = false
      // 刷新界面
      this.$refs.gainForm.reset()
    },
    // 导出详情
    exportExcel() {
      const id = this.$attrs['duty-id']
      exportDetail(id).then(res => {
        const content = res.data
        const blob = new Blob([content])
        const fileName = '盘点详细.xlsx'
        if ('download' in document.createElement('a')) { // 非IE下载
          const elink = document.createElement('a')
          elink.download = fileName
          elink.style.display = 'none'
          elink.href = URL.createObjectURL(blob)
          document.body.appendChild(elink)
          elink.click()
          URL.revokeObjectURL(elink.href) // 释放URL 对象
          document.body.removeChild(elink)
        } else { // IE10+下载
          navigator.msSaveBlob(blob, fileName)
        }
      })
    },
    /** 文件上传成功 */
    handleSuccess(response, file, fileList) {
      if (response.status === 'failure') {
        this.$message({
          showClose: true,
          message: response.data,
          type: 'error'
        })
      } else {
        this.$message({
          showClose: true,
          message: '导入完成',
          type: 'success'
        })
        this.refresh()
      }
    },
    /** 文件上传失败 */
    handleError(err, file, fileList) {
      console.log(err)
    }
  }

}
</script>
